package org.itboys.core.controller.front.small;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.util.JSON;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.count.SundayCountDto;
import org.itboys.core.dto.goods.SundayGoodsSizeDto;
import org.itboys.core.entity.mongo.SundayAddress;
import org.itboys.core.service.mongo.front.SundayFrontCartService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/10/15
 * 描述：购物车
 */
@Controller
@RequestMapping("/sunday/small/cart")
@Api(value = "cart", description = "购物车")
public class SundaySmallCartController extends BaseController {
    @Autowired
    private SundayFrontCartService cartService;

    /**
     * 加入购物车
     *
     * @param request
     * @param response
     * @param memberId
     * @param sceneId
     * @param num
     * @param cartType
     */
    @RequestMapping(value = "/addCart", method = {RequestMethod.POST})
    @ApiOperation(value = "加入购物车（仅支持单个商品）")
    public void addCart(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户ID") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(value = "场景ID") @RequestParam(value = "sceneId", required = true) Long sceneId,
            @ApiParam(value = "数量") @RequestParam(value = "num", required = true) Integer num,
            @ApiParam(value = "加入购车类型。1,加入购物车，2立即购买加入购物车。") @RequestParam(value = "cartType", required = true) Integer cartType,
            @ApiParam(value = "商品价格集合") @RequestParam(value = "goodsPrices", required = true) List<Double> goodsPrices,
            @ApiParam(value = "商品数量集合") @RequestParam(value = "goodsNums", required = true) List<Integer> goodsNums,
            @ApiParam(value = "商品id集合") @RequestParam(value = "goodsIds", required = true) List<String> goodsIds,
            @ApiParam(value = "商品尺码id集合") @RequestParam(value = "goodsCodeIds", required = true) List<String> goodsCodeIds,
            @ApiParam(value = "商品code集合") @RequestParam(value = "goodsCodes", required = true) List<String> goodsCodes
    ) {
        Long cartId=cartService.addCart(memberId, sceneId, num, cartType,goodsPrices,goodsNums,
                goodsIds,goodsCodeIds,goodsCodes);
        ResponseMessage.success(cartId);
    }

    @RequestMapping(value = "/getCart", method = {RequestMethod.POST})
    @ApiOperation(value = "获取购物车")
    public void getCart(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户ID") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(value = "加入购车类型。1,加入购物车，2立即购买加入购物车。") @RequestParam(value = "cartType", required = false) Integer cartType,
            @ApiParam(value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3") @RequestParam(value = "cartIds", required = false) List<Long> cartIds
    ) {
        SundayCountDto result = cartService.getCart(memberId, cartIds, cartType,SundayCommonConstants.NO);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/updateCart", method = {RequestMethod.POST})
    @ApiOperation(value = "更新购物车")
    public void updateCart(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "购物车ID") @RequestParam(value = "cartId", required = true) Long cartId,
            @ApiParam(value = "增减的数量") @RequestParam(value = "num", required = true) Integer num,
            @ApiParam(value = "增减类型 1 加   0 减") @RequestParam(value = "type", required = true) Integer type
    ) {
        cartService.updateCart(cartId, num,type);
        ResponseMessage.success();
    }


    /**
     * 删除购物车
     * @param request
     * @param response
     * @param memberId
     * @param cartIds
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ApiOperation(value = "删除购物车（支持批量删除）。")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户ID") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3") @RequestParam(value = "cartIds", required = true) List<Long> cartIds
    ) {
        cartService.deleteCart(cartIds, memberId, SundayCommonConstants.cart_type_cart);
        ResponseMessage.success();
    }

}
